package com.jubaozan.framework.sentinel.constant;

import cn.hutool.extra.spring.SpringUtil;
import com.jubaozan.framework.sentinel.generator.InstanceIdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author jiangjinwu
 * @version 1.0
 * @since 2025/7/15 15:07
 */

@Slf4j
@Component
public class RuleKeyConstant {
    private static String SYSTEM_RULE_KEY = "sentinel:%s:%s:rules:system";
    private static String PARAM_FLOW_RULE_KEY = "sentinel:%s:%s:rules:param";
    private static String FLOW_RULE_KEY = "sentinel:%s:%s:rules:flow";
    private static String DEGRADE_RULE_KEY = "sentinel:%s:%s:rules:degrade";
    private static String AUTHORITY_RULE_KEY = "sentinel:%s:%s:rules:authority";
    static String appName = SpringUtil.getProperty("spring.application.name");
    static String channel = "sentinel.rules.channel";

    @Autowired
    InstanceIdGenerator instanceIdGenerator;

    public RuleKeyConstant(){

    }

    public   String getFlowRuleKey() {
          String instanceId = instanceIdGenerator.getInstanceId();

        return String.format(FLOW_RULE_KEY, appName,instanceId);
    }

    public   String getSystemRuleKey() {
          String instanceId = instanceIdGenerator.getInstanceId();
        String appName = SpringUtil.getProperty("spring.application.name");
        return String.format(SYSTEM_RULE_KEY, appName,instanceId);
    }

    public   String getParamFlowRuleKey() {
        String instanceId = instanceIdGenerator.getInstanceId();
        String appName = SpringUtil.getProperty("spring.application.name");
        return String.format(PARAM_FLOW_RULE_KEY, appName,instanceId);
    }

    public   String getDegradeRuleKey() {
        String instanceId = instanceIdGenerator.getInstanceId();
        String appName = SpringUtil.getProperty("spring.application.name");
        return String.format(DEGRADE_RULE_KEY, appName,instanceId);
    }

    public String getServerTransportConfigKey(){
        String appName = SpringUtil.getProperty("spring.application.name");
        return String.format("%s-%s", appName,"serverTransportConfig");
    }

    public String getClusterClientConfigKey(){
        String appName = SpringUtil.getProperty("spring.application.name");
        return String.format("%s-%s", appName,"ClusterClientConfig");
    }


    public String getAuthorityRuleKey(){
        String instanceId = instanceIdGenerator.getInstanceId();
        String appName = SpringUtil.getProperty("spring.application.name");
        return String.format(AUTHORITY_RULE_KEY, appName,instanceId);
    }

    public String getChannel(){
        return channel;
    }
}
